package org.iks.user.web.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.iks.common.AjaxResult;
import org.iks.common.util.JwtUtil;
import org.iks.common.util.ServletUtil;
import org.iks.user.web.service.FriendService;
import org.iks.user.web.vo.UserFriendRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Author kzou
 * Date 2024/2/13
 * Description:
 */
@Tag(name = "好友模块")
@RestController
@RequestMapping("friend")
public class FriendController {
    @Autowired
    public FriendService friendService;


    @Operation(summary = "添加好友")
    @PostMapping("add")
    public AjaxResult<Void> addFriend(@RequestParam @Parameter(required = true, description = "用户账号") String account) {
        friendService.addFriend(account);
        return AjaxResult.success();
    }

    @Operation(summary = "获取好友列表")
    @PostMapping("query/list")
    public AjaxResult<List<UserFriendRespVo>> queryFriends() {
        String token = ServletUtil.getToken();
        String userAccount = JwtUtil.getUserAccount(token);
        List<UserFriendRespVo> result = friendService.queryFriends(userAccount);
        return AjaxResult.success(result);
    }

    @Operation(summary = "删除好友列表")
    @PostMapping("delete")
    public AjaxResult<?> deleteFriend(@RequestParam("friendAccount") String account) {
        String token = ServletUtil.getToken();
        String creatorAccount = JwtUtil.getUserAccount(token);
        friendService.deleteFriend(creatorAccount, account);
        return AjaxResult.success();
    }

}
